package clientSide.javaGui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.ObjectOutputStream;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;

import org.json.simple.JSONObject;

import static serverSide.Server.*;

public class GamePanels {
	public static class TictactoiPanel extends JPanel implements MouseListener {
		private int[] choices = {0, 0, 0,
				0, 0, 0,
				0, 0, 0};
		private boolean isTurn;
		
		public TictactoiPanel(boolean isTurn) {
			this.isTurn = isTurn;
			this.setBorder(new LineBorder(Color.RED,3));
			this.addMouseListener(this);
			this.setBackground(Color.WHITE);
		}
		
		private void drawCircle(Graphics2D g,int i) {
			g.setStroke(new BasicStroke(5));
			g.drawOval(5+(130/2)+(130*(i%3))-40, 5+(130/2)+(130*(i/3))-40, 80, 80);
		}
		private void drawCross(Graphics2D g,int i) {
			g.setStroke(new BasicStroke(5));
			g.drawLine(5+20+(i%3)*130, 5+20+(i/3)*130, 135-20+(i%3)*130, 135-20+(i/3)*130);
			g.drawLine(135-20+(i%3)*130, 5+20+(i/3)*130, 5+20+(i%3)*130, 135-20+(i/3)*130);
		}
		
		@Override
		public void paint(Graphics g) {
			super.paint(g);
			
			Graphics2D g2 = (Graphics2D)g;
			g2.setColor(Color.RED);
			g2.drawLine(135, 5, 135, getHeight()-5);
			g2.drawLine(265, 5, 265, getHeight()-5);
			g2.drawLine(5, 135, getWidth()-5, 135);
			g2.drawLine(5, 265, getWidth()-5, 265);
			
			for (int i = 0; i < 9; i++)
				if(choices[i] != 0) {
					if(choices[i] == 1)
						drawCircle(g2, i);
					else
						drawCross(g2, i);
				}
			
			if (!isTurn) {
				g2.setColor(Color.BLACK);
				g2.drawString("Not your turn!", (getWidth()/2)-40, (getHeight()/2));
			}
		}
		
		public void sendChoice(int i) {
			isTurn = false;
			
			JSONObject out = new JSONObject();
			out.put(SUBJECT_KEY, SUBJECT_VALUE_GAMING);
			out.put(SUBJECT_GAMING_KEY_ACTION, SUBJECT_GAMING_VALUE_GAME);
			out.put(SUBJECT_GAMING_KEY_GAMEACTION, i);
			
			((GamePage)getParent().getParent().getParent().getParent()).sendAction(out);
		}
		public void reciveChoice(int i) {
			choices[i] = 2;
			isTurn = true;
			repaint();
		}
		
		@Override
		public void mouseClicked(MouseEvent arg0) {
			if(!isTurn)
				return;
			
			int ind = ((arg0.getY()-5)/130)*3;
			ind += (arg0.getX()-5)/130;
			
			if(choices[ind] == 0) {
				choices[ind] = 1;
				sendChoice(ind);
				repaint();
			}
		}
		@Override
		public void mouseEntered(MouseEvent arg0) {}
		@Override
		public void mouseExited(MouseEvent arg0) {}
		@Override
		public void mousePressed(MouseEvent arg0) {}
		@Override
		public void mouseReleased(MouseEvent arg0) {}
	}
	
	public static class ChatPanel extends JPanel {
		JTextArea incoming;
		JTextField outgoing;
		String playerName;
		
		public ChatPanel(String playerName) {
			this.setOpaque(false);
			this.playerName = playerName;
			this.setLayout(new FlowLayout());
			this.setBorder(new LineBorder(Color.RED,1));
			
			incoming = new JTextArea(20,20);
			incoming.setLineWrap(true);
			incoming.setWrapStyleWord(true);
			incoming.setEditable(false);
			JScrollPane qScroller = new JScrollPane(incoming) ;
			qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			this.add(qScroller);
			
			outgoing = new JTextField(15);
			this.add(outgoing);
			
			
			JButton tmpButton = new JButton("send");
			tmpButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					JSONObject out = new JSONObject();
					out.put(SUBJECT_KEY, SUBJECT_VALUE_GAMING);
					out.put(SUBJECT_GAMING_KEY_ACTION, SUBJECT_GAMING_VALUE_CHAT);
					String tmp = outgoing.getText();
					outgoing.setText("");
					out.put(SUBJECT_GAMING_VALUE_CHAT, tmp);
					
					((GamePage)getParent().getParent().getParent().getParent()).sendAction(out);
					
					incoming.append(ChatPanel.this.playerName+": "+tmp+"\n");
				}
			});
			this.add(tmpButton);
		}
		
		public void recive(String s,String playerName2) {
			incoming.append(playerName2+": "+s+"\n");
		}
	}
	
}
